package com.newtouch.cq.security.config.auth;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.classmate.TypeResolver;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.Operation;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ApiListingScannerPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import java.util.Arrays;
import java.util.List;

/**
 * @author Alex
 * @Description security相关接口
 * @time 2021/8/19 13:22
 */
@Component
public class SwaggerAddtion implements ApiListingScannerPlugin {
    /**
     * Implement this method to manually add ApiDescriptions
     * 实现此方法可手动添加ApiDescriptions
     *
     * @param context - Documentation context that can be used infer documentation context
     * @return List of {@link ApiDescription}
     * @see ApiDescription
     */
    @Override
    public List<ApiDescription> apply(DocumentationContext context) {
        Operation usernamePasswordOperation = new OperationBuilder(new CachingOperationNameGenerator())
                .method(HttpMethod.POST)
                .summary("用户名密码登录")
                .notes("username/password登录")
                .consumes(CollUtil.newHashSet(MediaType.APPLICATION_FORM_URLENCODED_VALUE)) // 接收参数格式
                .produces(CollUtil.newHashSet(MediaType.APPLICATION_JSON_VALUE)) // 返回参数格式
                .tags(CollUtil.newHashSet("权限相关接口"))
                .requestParameters(Arrays.asList(
                        new RequestParameterBuilder()
                                .description("用户名")
//                                .(new TypeResolver().resolve(String.class))
                                .name("username")
//                                .defaultValue("admin")
//                                .parameterType("query")
//                                .parameterAccess("access")
                                .required(true)
//                                .modelRef(new ModelRef("string"))
                                .build(),
                        new RequestParameterBuilder()
                                .description("密码")
//                                .type(new TypeResolver().resolve(String.class))
                                .name("password")
//                                .defaultValue("123456")
//                                .parameterType("query")
//                                .parameterAccess("access")
                                .required(true)
//                                .modelRef(new ModelRef("string"))
                                .build(),
                        new RequestParameterBuilder()
                                .description("验证码唯一标识")
//                                .type(new TypeResolver().resolve(String.class))
                                .name("randomKey")
//                                .defaultValue("666666")
//                                .parameterType("query")
//                                .parameterAccess("access")
                                .required(true)
//                                .modelRef(new ModelRef("string"))
                                .build(),
                        new RequestParameterBuilder()
                                .description("验证码")
//                                .type(new TypeResolver().resolve(String.class))
                                .name("code")
//                                .parameterType("query")
//                                .parameterAccess("access")
                                .required(true)
//                                .modelRef(new ModelRef("string"))
                                .build()
                ))
                .responses(CollUtil.newHashSet(
                        new ResponseBuilder().code("200").description("ok")
                               .build()
                       ))
                .build();

        ApiDescription loginApiDescription = new ApiDescription("auth", "/auth/token", "登录接口","登录接口",
                Arrays.asList(usernamePasswordOperation), false);

        return Arrays.asList(loginApiDescription);
    }

    /**
     * 是否使用此插件
     *
     * @param documentationType swagger文档类型
     * @return true 启用
     */
    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }
}
